package com.hdaccp.lettingagency.controller;

import com.hdaccp.lettingagency.dao.HouseinfoDao;
import com.hdaccp.lettingagency.entity.Houseinfo;
import com.hdaccp.lettingagency.entity.Userinfo;
import com.hdaccp.lettingagency.pojo.HouseGroupXiaoQu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
@Scope("prototype")
public class HouseinfoController {
    @Autowired
    HouseinfoDao houseinfoDao  ;
    @RequestMapping("/query")
    @ResponseBody
    public List<Houseinfo> query(){

       // int result = 10 / 0 ;

        return houseinfoDao.query() ;
    }
    @RequestMapping(value = "/save/{id}/{username}/{phone}")
    public String save(@PathVariable String username) throws Exception{
        username = new String(username.getBytes("iso8859-1"),"utf-8") ;
        return "index" ;
    }
    @RequestMapping("/doup")
    @ResponseBody
    public Map<String,Object> doup(@RequestParam("myfile") MultipartFile myfile, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>() ;
        //得到上传文件的真实路径
        String path = request.getSession().getServletContext().getRealPath("/upimages") ;
        //得到上传文件名
        String fileName = myfile.getOriginalFilename() ;

        File file = new File(path + "\\" + fileName) ;

        try {
            myfile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return map ;
    }




    @RequestMapping(value = "/fileDownload")
    public ResponseEntity<byte[]> fileDownload(HttpServletRequest request, HttpServletResponse resp)
            throws Exception {
        // resp.setContentType("application/vnd.ms-excel;charset=utf-8");
        byte[] body = null;
        // ServletContext servletContext = request.getServletContext();
        // String fileName = path.substring(path.lastIndexOf("_") + 1); //从uuid_name.jpg中截取文件名
        //String path = request.getSession().getServletContext().getRealPath("1.txt");
        File file = new File("1.jpg");
        InputStream in = new FileInputStream(file);
        body = new byte[in.available()];
        in.read(body);
        HttpHeaders headers = new HttpHeaders();
        String fileName = new String("1.jpg".getBytes("gbk"), "iso8859-1");
        headers.add("Content-Disposition", "attachment;filename=" + fileName);
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);

        in.close();
        return response;
    }

    @RequestMapping("/queryGroupXiaoQu")
    @ResponseBody
    public List<HouseGroupXiaoQu> queryGroupXiqoQu(){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return houseinfoDao.queryGroupXiqoQu() ;
    }
}
